local hx__qinzheng = fk.CreateSkill {
  name = "hx__qinzheng",
  tags = { Skill.Compulsory },
}

local HX = require "packages.konglong.hxfs2"

hx__qinzheng:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@" .. hx__qinzheng.name, 0)
end)

local hx__qinzheng_spec = {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__qinzheng.name) and
      not table.every({ 2, 4, 6 }, function(num)
        return player:getMark("@" .. hx__qinzheng.name) % num ~= 0
      end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local loopList = table.filter({ 2, 4, 6 }, function(num)
      return player:getMark("@" .. hx__qinzheng.name) % num == 0
    end)

    local cards = {}
    for _, count in ipairs(loopList) do
      if count == 2 then
        local choice1 = player.room:askToChoice(player, {
          choices = {"slash", "jink"},
          skill_name = hx__qinzheng.name,
          cancelable = false,
        })
        HX.GetCardFromPile(room, player, choice1, hx__qinzheng.name)
      elseif count == 4 then
        local choice2 = player.room:askToChoice(player, {
          choices = {"peach", "analeptic"},
          skill_name = hx__qinzheng.name,
          cancelable = false,
        })
        HX.GetCardFromPile(room, player, choice2, hx__qinzheng.name)
      elseif count == 6 then
        local choice3 = player.room:askToChoice(player, {
          choices = {"ex_nihilo", "duel"},
          skill_name = hx__qinzheng.name,
          cancelable = false,
        })
        HX.GetCardFromPile(room, player, choice3, hx__qinzheng.name)
      end
    end
  end,
}
hx__qinzheng:addEffect(fk.CardUsing, hx__qinzheng_spec)
hx__qinzheng:addEffect(fk.CardResponding, hx__qinzheng_spec)

local hx__qinzheng_refresh_spec = {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__qinzheng.name)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "@" .. hx__qinzheng.name, 1)
  end,
}
hx__qinzheng:addEffect(fk.CardUsing, hx__qinzheng_refresh_spec)
hx__qinzheng:addEffect(fk.CardResponding, hx__qinzheng_refresh_spec)

return hx__qinzheng
